package com.cskaoyan.service.wxservice.impl;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.chaosbean.goods.vo.WxGoodsCategoryVo;
import com.cskaoyan.bean.chaosbean.goods.vo.WxGoodsDetailVo;
import com.cskaoyan.bean.chaosbean.goods.vo.WxGoodsListVo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.wxservice.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author dengqwe
 * @datetime 2023/1/2 13:48
 * @description: TODO:
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketGrouponMapper grouponMapper;
    @Autowired
    MarketIssueMapper issueMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    @Autowired
    MarketSystemMapper systemMapper;
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketCommentMapper commentMapper;

    @Override
    public int count() {
        //先从goods表中查出所有数据
        MarketGoodsExample example = new MarketGoodsExample();
        example.createCriteria().andDeletedEqualTo(false);
        long num = goodsMapper.countByExample(example);
        return (int) num;

    }

    @Override
    public WxGoodsCategoryVo category(Integer id) {
        WxGoodsCategoryVo wxGoodsCategoryVo = new WxGoodsCategoryVo();
        MarketCategoryExample example = new MarketCategoryExample();
        //通过主键拿到了当前category的id，可能是一级目录的id，也有可能是二级目录的id
        MarketCategory category = categoryMapper.selectByPrimaryKey(id);
        //对category的pid进行判断，如果pid==0，把category赋给parent
        //                       如果pid！=0，把category赋给current，那就是二级目录id
        if (category.getPid() == 0) {
            MarketCategory parent = category;
            example.clear();
            example.createCriteria().andPidEqualTo(category.getId()).andDeletedEqualTo(false);
            List<MarketCategory> blist = categoryMapper.selectByExample(example);
            wxGoodsCategoryVo.setBrotherCategory(blist);
            wxGoodsCategoryVo.setParentCategory(parent);
            wxGoodsCategoryVo.setCurrentCategory(blist.get(0));
        } else {
            example.createCriteria().andDeletedEqualTo(false).andIdEqualTo(category.getPid());
            MarketCategory Lev1category = categoryMapper.selectByPrimaryKey(category.getPid());
            example.clear();
            example.createCriteria().andDeletedEqualTo(false).andPidEqualTo(Lev1category.getId());
            List<MarketCategory> blist = categoryMapper.selectByExample(example);
            MarketCategory current = category;
            wxGoodsCategoryVo.setCurrentCategory(current);
            wxGoodsCategoryVo.setParentCategory(Lev1category);
            wxGoodsCategoryVo.setBrotherCategory(blist);

        }

        return wxGoodsCategoryVo;
    }

    @Override
    public WxGoodsListVo list(BasePageInfo info, Integer categoryId) {
        //    private CommonData<MarketGoods> categoryCommonData;
        //    private List<MarketCategory> filterCategoryList;
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        WxGoodsListVo wxGoodsListVo = new WxGoodsListVo();

        MarketCategoryExample example = new MarketCategoryExample();
        //通过传进了的二级目录的id，查到一级目录id，通过id把相应的二级目录封装成一个list  filterCategoryList
        MarketCategory lev1Category = categoryMapper.selectByPrimaryKey(categoryId);
        example.createCriteria().andDeletedEqualTo(false).andIdEqualTo(lev1Category.getId());
        List<MarketCategory> fList = categoryMapper.selectByExample(example);
        wxGoodsListVo.setFilterCategoryList(fList);


        CommonData<MarketGoods> data = new CommonData<>();
        MarketGoodsExample example1 = new MarketGoodsExample();
        example1.createCriteria().andDeletedEqualTo(false).andCategoryIdEqualTo(categoryId);
        List<MarketGoods> goodsList = goodsMapper.selectByExample(example1);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goodsList);

        wxGoodsListVo.setList(goodsList);

        wxGoodsListVo.setLimit(pageInfo.getPageSize());
        wxGoodsListVo.setPage(pageInfo.getPageNum());
        wxGoodsListVo.setPages(pageInfo.getPages());
        wxGoodsListVo.setTotal((int) pageInfo.getTotal());


        wxGoodsListVo.setFilterCategoryList(fList);
        wxGoodsListVo.setCategoryCommonData(data);

        return wxGoodsListVo;
    }

    @Override
    public WxGoodsDetailVo detail(Integer gid) {
        //new了一个返回对象
        WxGoodsDetailVo detail = new WxGoodsDetailVo();
        //在这里在goods表中查到该商品的信息
        MarketGoods good1 = goodsMapper.selectByPrimaryKey(gid);


        //为attribute赋值
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        attributeExample.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(gid);
        List<MarketGoodsAttribute> attributeList = attributeMapper.selectByExample(attributeExample);
        detail.setAttribute(attributeList);


        //为brand赋值
        MarketBrandExample example = new MarketBrandExample();
        example.createCriteria().andDeletedEqualTo(false).andIdEqualTo(good1.getBrandId());
        //这里其实只有一个
        List<MarketBrand> brand = brandMapper.selectByExample(example);
        for (MarketBrand marketBrand : brand) {
            detail.setBrand(marketBrand);
        }


        //为groupon赋值
        ArrayList<MarketGroupon> groupons = new ArrayList<>();
        detail.setGroupon(groupons);



        //为info赋值
        detail.setInfo(good1);


        //为issue赋值
        MarketIssueExample example1 = new MarketIssueExample();
        example1.createCriteria().andDeletedEqualTo(false);
        List<MarketIssue> issueList = issueMapper.selectByExample(example1);
        detail.setIssue(issueList);


        //为productionList赋值
        MarketGoodsProductExample example2 = new MarketGoodsProductExample();
        example2.createCriteria().andDeletedEqualTo(false).andGoodsIdEqualTo(gid);
        List<MarketGoodsProduct> productList = productMapper.selectByExample(example2);
        detail.setProductList(productList);


        //为share赋值
        MarketSystemExample example3 = new MarketSystemExample();
        example3.createCriteria().andKeyNameLike("%market_wx_share%");
        //其实只有一个值
        List<MarketSystem> systems = systemMapper.selectByExample(example3);
        for (MarketSystem system : systems) {
            String keyValue = system.getKeyValue();
            if ("true".equals(keyValue)) {
                detail.setShare(false);
            } else if ("false".equals(keyValue)) {
                detail.setShare(true);
            }
        }



        //为collection赋值
        MarketCollectExample example5 = new MarketCollectExample();
        example5.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(1).andTypeEqualTo((byte) 0)
                .andValueIdEqualTo(gid);

        List<MarketCollect> collectList = collectMapper.selectByExample(example5);
        if (collectList.isEmpty()) {
            detail.setUserHasCollect(0);
        } else {
            detail.setUserHasCollect(1);
        }


        //为shareImage赋值  ”“
        detail.setShareImage("");

        //private List<SpecificationListDTO> specificationList;  多规格
        //        private String name;     规格名
        //        private List<MarketGoodsSpecification> valueList;   相同规格的 不同种类
        MarketGoodsProductExample example6 = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria = example6.createCriteria();
        criteria.andGoodsIdEqualTo(gid);
        criteria.andDeletedEqualTo(false);
        List<MarketGoodsProduct> products = productMapper.selectByExample(example6);
        MarketGoodsProduct goodsProduct = products.get(0);

        String[] strings = goodsProduct.getSpecifications();  //两个值

        List<String> strings1 = new LinkedList<>();
        for (String string : strings) {
            MarketGoodsSpecificationExample example7 = new MarketGoodsSpecificationExample();
            MarketGoodsSpecificationExample.Criteria criteria1 = example7.createCriteria();
            criteria1.andGoodsIdEqualTo(gid).andValueEqualTo(string);
            List<MarketGoodsSpecification> list = specificationMapper.selectByExample(example7);
            MarketGoodsSpecification goodsSpecification = list.get(0);
            //specification 获取了第一个规格名
            String specification = goodsSpecification.getSpecification();
            //将规格名 加入list
            strings1.add(specification);
        }
         //strings1 规则 颜色
        List<WxGoodsDetailVo.SpecificationListDTO> listDTOS = new LinkedList<>();


        for (String s : strings1) {
            MarketGoodsSpecificationExample example7 = new MarketGoodsSpecificationExample();
            MarketGoodsSpecificationExample.Criteria criteria1 = example7.createCriteria();
            criteria1.andGoodsIdEqualTo(gid).andSpecificationEqualTo(s).andDeletedEqualTo(false);
            List<MarketGoodsSpecification> specifications = specificationMapper.selectByExample(example7);
            WxGoodsDetailVo.SpecificationListDTO dto = new WxGoodsDetailVo.SpecificationListDTO();
            dto.setName(s);
            dto.setValueList(specifications);
            listDTOS.add(dto);
        }
        detail.setSpecificationList(listDTOS);


        //为comment赋值
        WxGoodsDetailVo.CommentDTO commentDTO = new WxGoodsDetailVo.CommentDTO();
        //为count赋值
        MarketCommentExample example4 = new MarketCommentExample();
        example4.createCriteria().andDeletedEqualTo(false).andTypeEqualTo((byte) 0).andValueIdEqualTo(gid);
        long l = commentMapper.countByExample(example4);
        commentDTO.setCount((int) l);
        //为List<DataDTO>赋值
        example4.clear();
        example4.createCriteria().andDeletedEqualTo(false).andTypeEqualTo((byte) 0)
                .andValueIdEqualTo(gid);
       PageHelper.startPage(1,2);
        List<MarketComment> marketComments = commentMapper.selectByExample(example4);
        commentDTO.setData(marketComments);
        detail.setComment(commentDTO);
        return detail;
    }

    @Override
    public CommonData<MarketGoods> related(Integer id) {
        CommonData<MarketGoods> data = new CommonData<>();
        MarketGoodsExample example1 = new MarketGoodsExample();
        example1.createCriteria().andDeletedEqualTo(false);
        List<MarketGoods> goodsList = goodsMapper.selectByExample(example1);
        //随机从数据库中查六条数据，要不然就是始终就是数据库中的排序的前六个
        Random random = new Random();
        ArrayList<MarketGoods> goodsList1 = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            goodsList1.add(goodsList.remove(random.nextInt(goodsList.size())));
        }
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goodsList1);
        data.setTotal((int) pageInfo.getTotal());
        data.setLimit(pageInfo.getPageSize());
        data.setPage(pageInfo.getPageNum());
        data.setPages(pageInfo.getPages());
        data.setList(goodsList1);
        return data;
    }
}
